package q1.project.modules.system.dept.dao;

import java.util.List;
import java.util.Optional;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.Query;
import org.springframework.data.querydsl.QuerydslPredicateExecutor;
import org.springframework.stereotype.Repository;
import q1.project.modules.system.dept.entity.DeptEntity;

/**
 * 部门数据访问对象
 *
 * @author Q1 Team
 * @version 1.0
 */
@Repository
public interface DeptDao
    extends JpaRepository<DeptEntity, String>, QuerydslPredicateExecutor<DeptEntity> {

  Optional<DeptEntity> findByDeptCode(String deptCode);

  List<DeptEntity> findByParentIdOrderBySortOrder(String parentId);

  List<DeptEntity> findByStatusOrderBySortOrder(Integer status);

  @Query("SELECT d FROM DeptEntity d WHERE d.parentId IS NULL ORDER BY d.sortOrder")
  List<DeptEntity> findRootDepts();

  @Query("SELECT d FROM DeptEntity d WHERE d.parentId = ?1 AND d.status = 1 ORDER BY d.sortOrder")
  List<DeptEntity> findEnabledChildDepts(String parentId);

  long countByParentId(String parentId);
}
